window.onload = function () {

    var sltCity = document.getElementById('sltCity');

      initSelect('sltCity',cities);
      sltCity.onchange = sltCityChange;
}

/**
 *  selector : 选择器的文本
 *  items: 是一个数组，是select初始化的数据源
 *
 *  封装 ---》 复用，简化逻辑，维护代价低
 * */
function initSelect(selector,items) {

    var slt = document.getElementById(selector);

    for (var i in items) {

        var opt = document.createElement('option');
        opt.value = items[i].id;
        opt.innerText = items[i].name;

        slt.append(opt);
    }
}


function sltCityChange() {

    var selectedValue = this.selectedOptions[0].value;
    var schools = cities.find(function (city) {
        return city.id == selectedValue;
    }).schools;

    initSelect('sltSchool',schools);

}

